﻿using UnityEngine;
using System.Collections;

public class MusicManager : MonoBehaviour {

	public MusicManagerData musicManagerData;
	public AudioClip tempSoundEffectClip;
	public AudioClip tempVoiceClip;

	//Use THIS for initialization
	void Awake()
	{
		//musicManagerData = new MusicManagerData();
		// or
		musicManagerData = GameObject.Find("DataManager").GetComponent<MusicManagerData>();
		musicManagerData.BGM = gameObject.AddComponent<AudioSource>();
		musicManagerData.SoundEffect = gameObject.AddComponent<AudioSource>();
		musicManagerData.Voice = gameObject.AddComponent<AudioSource>();
		musicManagerData.tempSoundEffect = gameObject.AddComponent<AudioSource>();
		musicManagerData.tempVoice = gameObject.AddComponent<AudioSource>();



		/*SoundEffect = gameObject.AddComponent<AudioSource>();
		Voice = gameObject.AddComponent<AudioSource>();
		_tempSoundEffect = gameObject.AddComponent<AudioSource>();
		_tempVoice = gameObject.AddComponent<AudioSource>();

		BGM.playOnAwake = true;
		BGM.loop = true;*/
	}

	// Use this for initialization
	void Start () {
		musicManagerData.BGM.playOnAwake = true;
		musicManagerData.BGM.loop = true;
		musicManagerData.tempSoundEffect.clip = tempSoundEffectClip;
		musicManagerData.tempVoice.clip = tempVoiceClip;

		/*BGMVolume = GameObject.Find("MusicVolume").GetComponent<UISlider>();
		SoundVolume = GameObject.Find("SoundVolume").GetComponent<UISlider>();
		VoiceVolume = GameObject.Find("VoiceVolume").GetComponent<UISlider>();
		_tempSoundEffect.clip = tempSoundEffectClip;
		_tempVoice.clip = tempVoiceClip;*/
	}
	
	// Update is called once per frame
	void Update () {

	}

	public void PlayBGM(AudioClip clip)
	{
		musicManagerData.BGM.clip = clip;
		musicManagerData.BGM.Play();
		/*BGM.clip = clip;
		BGM.Play ();*/
	}

	public void PlaySoundEffect(AudioClip clip)
	{
		musicManagerData.SoundEffect.clip = clip;
		musicManagerData.SoundEffect.Play(); 
		/*SoundEffect.clip = clip;
		SoundEffect.Play();*/
	}

	public void PlayVoice(AudioClip clip)
	{
		musicManagerData.Voice.clip = clip;
		musicManagerData.Voice.Play();
		/*Voice.clip = clip;
		Voice.Play();*/
	}

	public void BGMAdjustVolume(float sliderValue)
	{
		musicManagerData.pauseInTime = musicManagerData.BGM.time;
		musicManagerData.BGM.volume = sliderValue;
		if(musicManagerData.BGM.volume > 0)
		{
			musicManagerData.BGM.mute = false;
			musicManagerData.BGM.PlayScheduled(musicManagerData.pauseInTime);
		}
		else
		{
			musicManagerData.pauseInTime = musicManagerData.BGM.time;
			musicManagerData.BGM.Pause();
			musicManagerData.BGM.mute = true;
		}

		/*BGM.volume = BGMVolume.sliderValue;
		if(BGM.volume > 0)
		{
			BGM.mute = false;
			BGM.PlayScheduled(_pauseInTime);
		}
		else
		{
			_pauseInTime = BGM.time;
			BGM.Pause();
			BGM.mute = true;
		}*/
	}

	public void SoundAdjustVolume(float sliderValue)
	{

		musicManagerData.SoundEffect.volume = sliderValue;
		musicManagerData.tempSoundEffect.volume = sliderValue;
		musicManagerData.tempSoundEffect.Play();

		/*SoundEffect.volume = SoundVolume.sliderValue;
		_tempSoundEffect.volume = SoundEffect.volume;
		_tempSoundEffect.Play();*/
	}

	public void VoiceAdjustVolume(float sliderValue)
	{

		musicManagerData.Voice.volume = sliderValue;
		musicManagerData.tempVoice.volume = sliderValue;
		musicManagerData.tempVoice.Play();
		/*Voice.volume = VoiceVolume.sliderValue;
		_tempVoice.volume = Voice.volume;
		_tempVoice.Play();*/
	}

	public void MuteAudio()
	{
		musicManagerData.BGM.mute = !musicManagerData.BGM.mute;
	}
}
